package cm.dpassyann.speedDating.model.dao.user.impl;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import roboguice.inject.InjectResource;
import cm.dpassyann.speedDating.R;
import cm.dpassyann.speedDating.exception.SpeedDatingException;
import cm.dpassyann.speedDating.model.bean.user.User;
import cm.dpassyann.speedDating.model.dao.user.IUserDao;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

/**
 * Pour le projet : amour-me-zam-android
 * @author deungoueyann
 * @description cette classe permet d'implémenter les appels CRUD du DAO User 
 * @date fait le : 22 nov. 2012
 */
public class UserDaoImpl implements IUserDao{

	@InjectResource(R.string.host_rest_service) String host;
	@InjectResource(R.string.user_connexion) String userConnexionPath;
	
	public List<User> list() {
		// TODO Auto-generated method stub
		return null;
	}

	public User get(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	public User connect(String login, String password) throws SpeedDatingException {
		StringBuilder builder = new StringBuilder();
		builder.append(host);
		builder.append(userConnexionPath);

		String httpRespStr;

		User user = null;

		JsonSerializer<Date> ser = new JsonSerializer<Date>() {
			public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext 
					context) {
				return src == null ? null : new JsonPrimitive(src.getTime());
			}
		};//permet de transformer un element date recu vers un json

		JsonDeserializer<Date> deser = new JsonDeserializer<Date>() {
			public Date deserialize(JsonElement json, Type typeOfT,
					JsonDeserializationContext context) throws JsonParseException {
				return json == null ? null : new Date(json.getAsLong());
			}
		};//methode inverse

		Gson gson = new GsonBuilder()
			.registerTypeAdapter(Date.class, ser)
			.registerTypeAdapter(Date.class, deser)
			.create();

		builder.append(login);
		builder.append("/");
		builder.append(password);
		
		HttpClient myHttpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(builder.toString());
		try {
			HttpResponse response = myHttpClient.execute(httpGet);
			int httpStatus = response.getStatusLine().getStatusCode();
			if(httpStatus == HttpStatus.SC_OK) {
				httpRespStr = EntityUtils.toString(response.getEntity());
				if(!"".equals(httpRespStr) && httpRespStr != null){
					user = gson.fromJson(httpRespStr, User.class);
					return user;
				}
				else{
					return null;
				}
			}
			else{
				return null;
			}
			
		} catch (Exception e) {
			throw new SpeedDatingException("Erreur de connexion");
		}

	}

}
